/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.main.configurator;

import it.gashale.jacolib.core.JacolibError;
import it.gashale.jacolib.util.FileSystem;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class SimplePropertiesEnvironment extends AbstractEnvironment {
	private String m_prop_file_name;
	private Properties m_int_prop=null;
	private Properties m_ext_prop=null;
	
	public SimplePropertiesEnvironment(String prop_file_name) throws JacolibError {
		super();
		m_prop_file_name=prop_file_name;
		init_internal_property_file();
		init_external_property_file();
	}
	
	private void init_internal_property_file() throws JacolibError {
		ClassLoader loader = ClassLoader.getSystemClassLoader();
		InputStream oInputStream = loader.getResourceAsStream (m_prop_file_name);
		if(oInputStream==null) {
			loader = SimplePropertiesEnvironment.class.getClassLoader();
			oInputStream = loader.getResourceAsStream (m_prop_file_name);
		}
		if(oInputStream==null) throw new JacolibError("Error opening \""+m_prop_file_name+"\" file.");
		m_int_prop = new Properties();
		try {
			m_int_prop.load(oInputStream);
		} catch (IOException e) {
			throw new JacolibError("Error loading \""+m_prop_file_name+"\" file. "+e.getMessage());
		}
	}
	private void init_external_property_file() throws JacolibError {
		String path=null;
		path=FileSystem.getJarDirectoryName(SimplePropertiesEnvironment.class);
		if(path!=null) {
			String filename=path+File.separatorChar+m_prop_file_name;
			File file=new File(filename);
			if(file.exists()) {
				FileInputStream inStream;
				try {
					inStream = new FileInputStream(file);
					m_ext_prop = new Properties();
					m_ext_prop.load(inStream);
				} catch (FileNotFoundException e) {
					throw new JacolibError("Error loading \""+m_prop_file_name+"\" file. "+e);
				} catch (IOException e) {
					throw new JacolibError("Error loading \""+m_prop_file_name+"\" file. "+e);
				}
			}
		}
	}
	protected String getEnv(String name) {
		return System.getenv(name);
	}
	protected String getProperty(String name) {
		String value=null;
		if(m_ext_prop!=null) {
			value=m_ext_prop.getProperty(name);
			if(value!=null && value.equals("")) value=null;
		}
		if(value==null) {
			value=m_int_prop.getProperty(name);
			if(value!=null && value.equals("")) value=null;
		}
		return value;
	}
	protected String getSystemProperty(String name) {
		return System.getProperty(name);
	}
	
	
}
